<?php
$startDate = $viewDate;
$endDate = $viewDate;
?>

<table>
   <tr>
      <th>Employee</th>
      <th>Role</th>
      <th>Start Time</th>
      <th>End Time</th>
      <th>Status</th>
      <th>Action</th>
   </tr>

   <?php
   $checkRangeScheduleResult = checkRangeSchedule($_SESSION['userName'], $_SESSION['userPassHash'], date("Y-m-d", $startDate), date("Y-m-d", $endDate));
   $employees = $checkRangeScheduleResult['employees'];

   //grab the accounts
   $getAccountsResult = getAccounts($_SESSION['userName'], $_SESSION['userPassHash']);
   $accounts = $getAccountsResult['results'];

   //grab the roles
   $getRolesResult = getRoles($_SESSION['userName'], $_SESSION['userPassHash']);
   $roles = $getRolesResult[1];

//parse the result
   foreach ($employees as $employee) {
      $shifts = $employee['shifts'];

      //display employee only if they have a shift that day
      if ($shifts != NULL) {
         ?>
         <tr>
            <td><?php echo($employee['accName']); ?></td>
            <td><?php echo($shifts[0]['roleName']); ?></td>
            <td><?php echo($shifts[0]['startTime']); ?></td>
            <td><?php echo($shifts[0]['endTime']); ?></td>
            <td><?php echo($shifts[0]['shiftStatus']); ?></td>
            <td>
               <a href="#mod<?php echo($shifts[0]['roleID'] . $shifts[0]['dateID'] . $employee['empID']); ?>" 
                  role="button" class="btn" data-toggle="modal">Modify</a>

               <!-- Modal -->
               <div id="mod<?php echo($shifts[0]['roleID'] . $shifts[0]['dateID'] . $employee['empID']); ?>" 
                    class="modal hide fade" role="dialog" 
                    aria-labelledby="myModalLabel" aria-hidden="true">

                  <div class="modal-header">
                     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                     <h5><?php echo(date("l, M d, Y", strtotime($shifts[0]['datestamp']))); ?></h5>
                     <h5><?php echo($employee['accName']); ?></h5>
                     <h5><?php echo($shifts[0]['roleName']); ?></h5>
                  </div>

                  <div class="modal-body">
                     <table>

                        <tr>
                        <form action="" method="post">
                           <td>
                              <input type="hidden" name="roleID" value="<?php echo($shifts[0]['roleID']); ?>">
                              <input type="hidden" name="dateID" value="<?php echo($shifts[0]['dateID']); ?>">
                              <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                              <input type="hidden" name="fieldToModify" value="dateID">
                              <label>Shift Date</label> 
                           </td>
                           <td>
                              <input type="date" name="newValue" value="<?php echo($shifts[0]['datestamp']); ?>" />
                           </td>
                           <td>
                              <input type="submit" name="action" value="Save" />
                           </td>
                        </form>
                        </tr>

                        <tr>
                        <form action="" method="post">
                           <td>
                              <input type="hidden" name="roleID" value="<?php echo($shifts[0]['roleID']); ?>">
                              <input type="hidden" name="dateID" value="<?php echo($shifts[0]['dateID']); ?>">
                              <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                              <input type="hidden" name="fieldToModify" value="empID">
                              <label>Employee</label> 
                           </td>
                           <td>
                              <select name="newValue">
                                 <?php
                                 foreach ($accounts as $account) {
                                    echo ("<option value=" . $account['accName']);
                                    if ($account['accName'] == $employee['accName']) {
                                       echo(" SELECTED ");
                                    }
                                    echo (">" . $account['accName'] . "</option>");
                                 }
                                 ?>
                              </select>
                           </td>
                           <td>
                              <input type="submit" name="action" value="Save" />
                           </td>
                        </form>
                        </tr>

                        <tr>
                        <form action="" method="post">
                           <td>
                              <input type="hidden" name="roleID" value="<?php echo($shifts[0]['roleID']); ?>">
                              <input type="hidden" name="dateID" value="<?php echo($shifts[0]['dateID']); ?>">
                              <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                              <input type="hidden" name="fieldToModify" value="roleID">
                              <label>Role</label> 
                           </td>
                           <td>
                              <select name="newValue">
                                 <?php
                                 foreach ($roles as $role) {
                                    echo ("<option value=" . $role['roleID']);
                                    if ($role['roleID'] == $shifts[0]['roleID']) {
                                       echo(" SELECTED ");
                                    }
                                    echo (">" . $role['roleName'] . "</option>");
                                 }
                                 ?>
                              </select>
                           </td>
                           <td>
                              <input type="submit" name="action" value="Save" />
                           </td>
                        </form>
                        </tr>

                        <tr>
                        <form action="" method="post">
                           <td>
                              <input type="hidden" name="roleID" value="<?php echo($shifts[0]['roleID']); ?>">
                              <input type="hidden" name="dateID" value="<?php echo($shifts[0]['dateID']); ?>">
                              <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                              <input type="hidden" name="fieldToModify" value="startTime">
                              <label>Start Time</label> 
                           </td>
                           <td>
                              <input type="time" name="newValue" value="<?php echo($shifts[0]['startTime']); ?>" />
                           </td>
                           <td>
                              <input type="submit" name="action" value="Save" />
                           </td>
                        </form>
                        </tr>

                        <tr>
                        <form action="" method="post">
                           <td>
                              <input type="hidden" name="roleID" value="<?php echo($shifts[0]['roleID']); ?>">
                              <input type="hidden" name="dateID" value="<?php echo($shifts[0]['dateID']); ?>">
                              <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                              <input type="hidden" name="fieldToModify" value="endTime">
                              <label>End Time</label> 
                           </td>
                           <td>
                              <input type="time" name="newValue" value="<?php echo($shifts[0]['endTime']); ?>" />
                           </td>
                           <td>
                              <input type="submit" name="action" value="Save" />
                           </td>
                        </form>
                        </tr>

                        <tr>
                        <form action="" method="post">
                           <td colspan="3">
                              <input type="hidden" name="roleID" value="<?php echo($shifts[0]['roleID']); ?>">
                              <input type="hidden" name="dateID" value="<?php echo($shifts[0]['dateID']); ?>">
                              <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                              <input type="submit" name="action" value="Delete Shift" />
                           </td>
                        </form>
                        </tr>

                     </table>
                  </div>

                  <div class="modal-footer">
                     <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                  </div>

               </div>
               <!-- Modal -->
            </td>
         </tr>
         <?php
      }
   }
   ?>
</table>
